package edu.suse.common.base;

import java.io.Serializable;
/**
 * 定义一些通用的返回结果的方法，例如返回成功结果的success方法、返回失败结果的error方法。
 */
public class RespResult<T> implements Serializable {

    /**
     * 结果码
     */
    private Integer code = RespResultCode.OK.getCode();

    /**
     * 结果信息
     */
    private String message = RespResultCode.OK.getMessage();

    /**
     * 详细的错误消息(开发看)
     */
    private String detailMessage = RespResultCode.OK.getDetailMessage();

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public String getDetailMessage() {
        return detailMessage;
    }

    public void setDetailMessage(String detailMessage) {
        this.detailMessage = detailMessage;
    }

    /**
     * 返回结果的数据对象
     */
    private T data;

    public RespResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public RespResult() {

    }

    public RespResult(Integer code) {
        this.code = code;
    }

    public RespResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public RespResult(IRespResultCode respResultCode) {
        this.code = respResultCode.getCode();
        this.message = respResultCode.getMessage();
    }

    public RespResult(Integer code, String message, String detailMessage) {
        this.code = code;
        this.message = message;
        this.detailMessage = detailMessage;
    }

    public static RespResult<Void> success() {
        return new RespResult<>(RespResultCode.OK);
    }

    public static <T> RespResult<T> success(T data) {
        RespResult<T> ar = new RespResult<T>(RespResultCode.OK);
        ar.setData(data);
        return ar;
    }

    public static <T> RespResult<T> error(String msg) {
        return new RespResult<T>(RespResultCode.SYS_EXCEPTION.getCode(), msg);
    }

    public static <T> RespResult<T> error(Integer code, String msg) {
        return new RespResult<T>(code, msg);
    }

    public static <T> RespResult<T> error(Integer code, String msg, String detailMessage) {
        return new RespResult<T>(code, msg, detailMessage);
    }

    public static <T> RespResult<T> error(Integer code, String msg, T data) {
        return new RespResult<T>(code, msg).setData(data);
    }
}
